package com.andy.wan.android.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.andy.ui.base.BaseActivity;
import com.andy.wan.android.R;
import com.andy.wan.android.mvp.api.WanAndroidApi;
import com.andy.wan.android.mvp.base.BaseObserver;
import com.andy.wan.android.mvp.bean.LoginBean;
import com.andy.wan.android.mvp.event.LoginEvent;
import com.andy.wan.android.mvp.net.RetrofitUtil;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.trello.rxlifecycle3.android.ActivityEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 作者:luofan
 * 创建时间:2020/2/27 11:45
 * 描述: 登录
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.img_back)
    ImageView mImgBack;
    @BindView(R.id.tv_title_center)
    TextView mTvTitleCenter;
    @BindView(R.id.edit_account)
    TextInputEditText mEditAccount;
    @BindView(R.id.layout_account)
    TextInputLayout mLayoutAccount;
    @BindView(R.id.edit_password)
    TextInputEditText mEditPassword;
    @BindView(R.id.layout_password)
    TextInputLayout mLayoutPassword;
    @BindView(R.id.tv_login)
    TextView mTvLogin;
    @BindView(R.id.tv_register)
    TextView mTvRegister;

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(mEditAccount.getText()) && !TextUtils.isEmpty(mEditPassword.getText())) {
                mTvLogin.setEnabled(true);
            } else {
                mTvLogin.setEnabled(false);
            }
        }
    };

    @Override
    public void initLogic() {
        mEditAccount.addTextChangedListener(mTextWatcher);
        mEditPassword.addTextChangedListener(mTextWatcher);
        mTvTitleCenter.setText(R.string.login);
        mImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }

    public void login() {
        String account = mEditAccount.getText().toString();
        String password = mEditPassword.getText().toString();
        Map<String, String> params = new HashMap<>();
        params.put("username", account);
        params.put("password", password);
        RetrofitUtil.getInstance().create(WanAndroidApi.class).login(params).compose(bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<LoginBean>() {

                    @Override
                    protected void onGetData(LoginBean data) {
                        SPUtils.getInstance().put(LoginBean.class.getName(), GsonUtils.toJson(data));
                        LoginEvent event = new LoginEvent();
                        EventBus.getDefault().post(event);
                        finish();
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    public static void startMe(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

}
